package dao.impl;

import dao.CommentDao;
import pojo.Comments;
import util.DataUtils;
import util.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class CommentDaoImpl implements CommentDao {
    private Connection conn = null;
    private ResultSet rs = null;
    private boolean flag = false;


    public ArrayList<Comments> findCommentByNid(Integer nid) throws SQLException, ClassNotFoundException {
        ArrayList<Comments> commentList = new ArrayList<>();
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select * from comments where nid='" + nid + "' and parent='" + 0 + "'");
        ResultSet rs2 = null;
        while (true) {
            if (!rs.next()) break;
            Comments comment = new Comments();
            comment.setNid(rs.getInt("nid"));
            comment.setUid(rs.getInt("uid"));
            comment.setComment(rs.getString("comment"));
            comment.setCid(rs.getInt("cid"));
            comment.setParent(rs.getInt("parent"));
            //comment= (Comments) DataUtils.dataEncapsulation(Class.forName("pojo.Comments"),rs);
            rs2 = JdbcUtils.selectsql("select usename from yonghu where uid='" + rs.getInt("uid") + "'");
            if (rs2.next()) {
                comment.setUsename(rs2.getString("usename"));
            }
            commentList.add(comment);
        }
        JdbcUtils.closeconn(rs, null, conn);
        JdbcUtils.closeconn(rs2, null, null);
        return commentList;

    }


    public boolean addComment1(Comments comments) throws SQLException {
        String usename = null;
        conn = JdbcUtils.dbconn();
        Integer i = 0;
        String sql2 = "select usename from yonghu where uid='" + comments.getUid() + "'";
        rs = JdbcUtils.selectsql(sql2);
        if (rs.next()) {
            usename = rs.getString("usename");
        }
        String sql = "insert into comments values('" + 0 + "','" + comments.getUid() + "','" + comments.getNid() + "','" + comments.getComment() + "','" + usename + "','" + 0 + "')";
        i = JdbcUtils.addsql(sql);
        if (i > 0) flag = true;
        JdbcUtils.closeconn(rs, null, conn);
        return flag;
    }


    public boolean userDeleteComment(Integer cid) throws SQLException {
        conn = JdbcUtils.dbconn();
        Integer i = 0;
        String sql = "delete from comments where cid='" + cid + "'";
        i = JdbcUtils.addsql(sql);
        if (i > 0) flag = true;
        JdbcUtils.closeconn(null, null, conn);
        return flag;
    }


    public Comments findCommentByCid(Integer cid) throws SQLException, ClassNotFoundException {
        conn = JdbcUtils.dbconn();
        Comments comment = new Comments();
        rs = JdbcUtils.selectsql("select * from comments where cid='" + cid + "'");
//        while (true) {
//                if (!rs.next()) break;
        comment = (Comments) DataUtils.dataEncapsulation(Class.forName("pojo.Comments"), rs);
//        }
        JdbcUtils.closeconn(rs, null, conn);
        return comment;
    }

    public ArrayList<Comments> findCommentChildByCid(Integer cid) throws SQLException, ClassNotFoundException {
        conn = JdbcUtils.dbconn();
        Comments comment = new Comments();
        rs = JdbcUtils.selectsql("select * from comments where cid='" + cid + "'");
//        while (true) {
//                if (!rs.next()) break;
        comment = (Comments) DataUtils.dataEncapsulation(Class.forName("pojo.Comments"), rs);
        // }
        JdbcUtils.closeconn(rs, null, conn);
        return null;
    }
}
